package com.chia.multienty.workflow.controller;

import com.chia.multienty.core.annotation.WebLog;
import com.chia.multienty.core.domain.basic.Result;
import com.chia.multienty.core.domain.enums.HttpResultEnum;
import com.chia.multienty.core.exception.KutaRuntimeException;
import com.chia.multienty.core.parameter.workflow.FlowableUserAddParameter;
import com.chia.multienty.core.parameter.workflow.FlowableUserDeleteParameter;
import com.chia.multienty.core.parameter.workflow.FlowableUserSearchParameter;
import com.chia.multienty.workflow.service.FlowableUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.crypto.CryptoException;
import org.flowable.idm.api.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final FlowableUserService flowableUserService;

    @PostMapping("list")
    @ApiOperation(value = "获取用户分页列表")
    public Result<List<User>> getList(@RequestBody FlowableUserSearchParameter parameter) {
        List<User> list = flowableUserService.list(parameter);
        return new Result<>(list, HttpResultEnum.SUCCESS.getCode());
    }

    @PostMapping("save")
    @ApiOperation(value = "保存用户")
    @WebLog(target = "Dict")
    public Result<Boolean> save(@RequestBody FlowableUserAddParameter parameter) throws CryptoException {
        flowableUserService.add(parameter);
        return new Result<>(true, HttpResultEnum.SUCCESS);
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除用户")
    @WebLog(target = "Dict")
    public Result<Boolean> delete(@RequestBody FlowableUserDeleteParameter parameter) throws KutaRuntimeException {
        flowableUserService.delete(parameter.getUserId());
        return new Result<>(true, HttpResultEnum.SUCCESS);
    }
}
